package com.mall.common.util;

import static com.mall.common.Constants.Search_prefix;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * 网店工具类
 * @author zhenglong
 */
public abstract class UrlUtils {

	//用于搜索的字段名字
	private static String[] searchFields = 
		new String[] {"goodsTag", "promotionType", "activity", "brand", "cat", "manufacturer", "goodsType", "drugform", 
			"prescribeType", "medicalInsuranceType", "stock", "sort", "page"};
	
	/**
	 * 获取url所带的参数(去掉url前缀)	list-cat-2-brand-2  转换后 cat-2-brand-2
	 * @param str
	 * @return
	 */
	public static String getParamStr(String str){
		String pattern = Search_prefix + "(.*)";
		if(str.startsWith(Search_prefix)){
			pattern = Search_prefix + "(.*)";
		}
//		String pattern = Search_prefix + "(.*)|plaza-(.*)";
		Pattern p = Pattern.compile(pattern, 2 | Pattern.DOTALL);
		Matcher m = p.matcher(str);
		String temp = str;
		if (m.find()) {
			temp = m.replaceAll("$1");
		}
		return temp;
	}
	
	/**
	 * 组合排列搜索条件，并组合成链接地址返回
	 * @param url
	 * @param name
	 * @param value
	 * @return
	 */
	public static String addUrlParam(String url, String name, String value, boolean noPage){
		String paramStr= getParamStr(url);
		if(paramStr == null || paramStr.equals("")){
			return url;
		}
		//组合新的URL
		StringBuffer newUrl = new StringBuffer(url.split("-")[0]);
		
		String temp = null;
		for(String field : searchFields){
			if("page".equals(field) || "stock".equals(field)){
				continue;
			}
			temp = getParamStringValue(paramStr, field);
			if(StringUtils.isNotEmpty(temp)){
				newUrl.append("-").append(field).append("-").append(temp);
			}
			if(name != null && name.equals(field)){
				newUrl.append("-").append(field).append("-").append(value);
			}
		}
		return newUrl.toString();
	}
	
	
	/**
	 * 解析一个搜索地址字串（在网店搜索页面中），根据传递的参数字串解析出参数值如：<br/>
	 * 有参数字串：cat{5}keyword{测试}<br/>
	 * 则cat参数值为:5,keyword参数值为:测试<br/>
	 * 
	 * @param url搜索地址字串
	 * @param name 参数名称
	 * @return
	 */
	public static String getParamStringValue(String param, String name) {
//		String pattern = "(.*)" + name + "\\{(.[^\\{]*)\\}(.*)";
		param = getParamStr(param);
		String pattern = "(.*)" + name + "\\-(.[^\\-]*)(.*)";
		String value = null;
		Pattern p = Pattern.compile(pattern, 2 | Pattern.DOTALL);
		Matcher m = p.matcher(param);
		if (m.find()) {
			value = m.replaceAll("$2");
		}
		return value;
	} 
	
	public static int getParamInitValue(String param, String name) {
		String temp_str = getParamStringValue(param,name);
		int value = Integer.valueOf(temp_str);
		return value;
	}
	
	/**
	 * 解析一个搜索地址字串（在网店搜索页面中），得到排除某个参数后的字串
	 * @param url 搜索字串
	 * @param name 要排除的字串
	 * @return
	 */
	public static String getExParamUrl(String url, String name){
//		String pattern = "(.*)" + name + "\\{(.[^\\{]*)\\}(.*)";
		String pattern = "(.*)" + name + "\\-(.[^\\-]*)(\\-|.*)(.*)";
		String value = "";
		Pattern p = Pattern.compile(pattern, 2 | Pattern.DOTALL);
		Matcher m = p.matcher(url);
		if (m.find()) {
			value = m.replaceAll("$1$4");
		}else {
			value = url;
		}
		if(value != null){
			if(value.startsWith("-")){
				value = value.substring(1, value.length());
			}
			if(value.endsWith("-")){
				value = value.substring(0, value.length() - 1);
			}
		}
		return value;
	}
	
	/**
	 * 操作一个搜索字串：向字串中添加参数值。<br/>
	 * 在原有参数值的基础上加入新的参数值，用“,”号隔开。
	 * @param url
	 * @param name
	 * @param value
	 * @return
	 */
	public static String appendParamValue(String url, String name, String value, boolean noPage){
		String old_value = getParamStringValue(url, name);
		String new_url = getExParamUrl(url,name);
		if(old_value != null){
			if("prop".equals(name)){ //对于属性字串的特殊处理
				old_value = old_value.replaceAll( value.split("_")[0] +"_(\\d+)", "");
				old_value = old_value.replace(",,", ",");
				if( old_value.startsWith(",") ){
					old_value = old_value.substring(1, old_value.length());
				}
				
				if(old_value.endsWith(",")){
					old_value = old_value.substring(0, old_value.length() - 1);
				}
			}
			if(!old_value.equals(""))
				value = "," + value;
		} else {
			old_value = "";
		}
//		new_url =new_url+name+"-"+old_value+value;
		new_url = addUrlParam(new_url, name, old_value + value, noPage);
		
		return new_url;
	}
	
	
	/**
	 * 解析一个伪静态url字串<br/>
	 * 得到这个伪静脉url字串的前缀<br/>
	 * 如：/goods_list_tag-tag_id{3}.html 得到goods_list_tag<br/>
	 * /search-cat_id{3}page{1}.html 得到search
	 * @param url
	 * @return
	 */
	public static String getUrlPrefix(String url){
		String pattern = "(.*)-(.*)";
		String value = null;
		Pattern p = Pattern.compile(pattern, 2 | Pattern.DOTALL);
		Matcher m = p.matcher(url);
		if (m.find()) {
			value = m.replaceAll("$1"); 
		}
		return value;
	}
	
	/**
	 * 获取排除某个属性后的属性字串
	 * @param index
	 * @param propStr
	 * @return
	 */
	public static String getPropExSelf(int index, String url){
		if(StringUtils.isEmpty(url)){
			return url;
		}
		String propstr = getParamStringValue(url, "prop");
		if(StringUtils.isEmpty(propstr)) {
			return url;
		}
		
		String newprostr ="";
		String[] propar = propstr.split(",");
		for(String prop:propar){
			String[] ar = prop.split("_");
			if( !ar[0].equals("" + index )){ //剔除掉这个属性
				if(!newprostr.equals(""))
					newprostr += ",";
				newprostr += prop;
			}
		}
		if(!StringUtils.isEmpty(newprostr)){
			url = url.replaceAll(propstr, newprostr);
		}else{
			url = url.replaceAll("-prop-" + propstr, "");
		}
		return url;
		
	}
	
	public static Map<String, String> getParams(String url) {
		Map<String, String> resultMap = new HashMap<String, String>();
		for(String searchField : searchFields) {
			String searchValue = getParamStringValue(url, searchField);
			if(StringUtils.isNotBlank(searchValue)) {
				resultMap.put(searchField, searchValue);
			}
		}
		return resultMap;
	}
	
/*	public static void main(String args[]){
		String str = UrlUtils.getParamStr("/list-cat-1");
		System.out.println(str);
		
		String cat = UrlUtils.getParamStringValue("list-cat-4", "cat");
		System.out.println(cat);
		
		String cat2 = UrlUtils.getParamStringValue("localhost:8080/wsshop/front/goods/goods!list.action?cat-4", "cat");
		System.out.println(cat2);
		System.out.println( "9_1,8_2,2_1".replaceAll("(,|2_(\\d+)", "")  );
		String url  ="cat{102}prop{2_1,1_2,0_3}name{2010}page{9}";
		String newUrl = "cat-102-page-9-prop-2_1,1_2,0_3";
		System.out.println( getPropExSelf(2,url) );
		System.out.println(getExParamUrl(newUrl,"cat"));
		System.out.println(getExParamUrl(newUrl,"prop"));
		System.out.println(getExParamUrl(newUrl,"name"));
		System.out.println(getExParamUrl(newUrl,"page"));
		url="search-cat{4}brand{13}prop{}.html";
		System.out.println( getExParamUrl(url, "prop") );
		String pattern = "(.*)\\-(.[^\\-]*)";
		String value = "";
		Pattern p = Pattern.compile(pattern, 2 | Pattern.DOTALL);
		Matcher m = p.matcher(newUrl);

		if (m.find()) {
			value = m.replaceAll("$1$2");
			System.out.println(value);
		}
		String temp = "/search-cat-1";
		System.out.println(addUrl(temp,"circlar","3"));
	}*/
}
